package com.bsh.controller.front;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.dto.FeedbackQryDto;
import com.bsh.mybatis.model.Feedback;
import com.bsh.service.FeedbackService;
import com.bsh.utils.PageJosnMessage;
import com.bsh.utils.PageTools;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * 系统消息
 * 
 * @author Administrator
 *
 */
@Controller
public class FeedBackController {
	private static Logger log = LoggerFactory.getLogger(FeedBackController.class);

	@Autowired
	HttpServletRequest request;
	@Autowired
	HttpServletResponse response;
	@Autowired
	private FeedbackService fbServ;

	/**
	 * 留言列表
	 */
	@RequestMapping(value = "/feedback/list")
	public ModelAndView bannerList(@ModelAttribute FeedbackQryDto dto)  {
		String title = request.getParameter("title") == null ? null : request.getParameter("title").toString();

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/front/feedbackList");		

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = fbServ.getPage(pager, dto);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("dto",dto);
		return mv;
	}
	

	/**
	 * 处理-初使化
	 */
	@RequestMapping(value = "/feedback/process/{msgid}",method=RequestMethod.GET)
	public ModelAndView process(@PathVariable(value="msgid") int id) {
		ModelAndView mv = new ModelAndView();	
		mv.setViewName("/pages/front/feedbackProcessMsg");
		Feedback res = fbServ.getMessage(id);
		mv.addObject("id",id);
		return mv;
	}
	
	/**
	 * 处理-保存
	 */
	@RequestMapping(value = "/feedback/process",method=RequestMethod.POST)
	public ModelAndView processSave(@RequestParam int id, @RequestParam String msg) {
		Integer res = fbServ.process(id, msg);
		
		ModelAndView mv = new ModelAndView();	
		mv.setViewName("/pages/front/feedbackProcessMsg");
		String resMsg = PageTools.alertCloseFraRefresh("修改首页banner",res > 0? "保存成功！":"保存失败", 1000);
		mv.addObject("actionMsg", resMsg);
		mv.addObject("id",id);
		mv.addObject("status", res > 0 ? 1 : 0);
		return mv;
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping(value = "/feedback/{id}",method=RequestMethod.DELETE)
	public PageJosnMessage delete(@PathVariable(value = "id") Integer id) {
		Integer res = fbServ.remove(id);
		PageJosnMessage msg = new PageJosnMessage();
		res = (res == null ? 1 : res);
		msg.putData("status", res > 0 ? 1 : 0);
		return msg;
	}
	
	/**
	 * 客服记录列表
	 */
	@RequestMapping(value = "/feedback/serviceRecordList/{mobile}")
	public ModelAndView serviceRecords(@PathVariable(value = "mobile") String mobile)  {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/front/serviceRecordList");		

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		FeedbackQryDto dto=new FeedbackQryDto();
		dto.setMobile(mobile);
		List<Map<String, Object>> list = fbServ.getServiceRecords(pager, dto);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		mv.addObject("dto",dto);
		return mv;
	}
	
	/**
	 * 客服记录-保存
	 */
	@RequestMapping(value = "/feedback/addRecord",method=RequestMethod.POST)
	public ModelAndView addRecord(@ModelAttribute FeedbackQryDto dto) {
		 BeanCopier copy = BeanCopier.create(FeedbackQryDto.class, Feedback.class, false);
		 Feedback fb=new Feedback();
		 copy.copy(dto,fb,null);
		 Date date=new Date();
		 fb.setCreateTime(date);
		 fb.setStype((short) 1);
		 fb.setStatus("0");
		 if(fb.getIsTop()!=null&&fb.getIsTop()==1){
			 fb.setSort(date);
		 }
		 int res=fbServ.addFeedBack(fb);
		 ModelAndView mv = this.serviceRecords(dto.getMobile());	
			
		 String msg = "";
		 if (res >0)
		    msg = PageTools.alertCloseFraRefresh("保存记录", "保存成功！", 1000);
		 else
			msg = PageTools.alert("修改记录", "保存失败，请稍后重试！", 1000);

		 mv.addObject("actionMsg", msg);
		 return mv;
	}
	
	
	
}
